﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ucenter
 * ==============================================================================*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Compilation;

using UCenter.Core.Common;

namespace UCenter.Core.Web
{
    public static class ResourceExtension
    {
        /// <summary>
        /// HtmlHelper Extend method of getting the resource string
        /// </summary>
        /// <param name="helper">The HtmlHelper source</param>
        /// <param name="expression">Resource expression, like "labels, UCenter" or "UCenter"</param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string Resource(this HtmlHelper helper, string expression, params object[] args)
        {
            string virtualPath = ViewUtils.GetVirtualPath(helper);

            return GetResource(helper.ViewContext.HttpContext,
                GetExpression(expression), virtualPath, args);
        }

        /// <summary>
        /// Controller extend method of getting the resource string
        /// </summary>
        /// <param name="controller">The Controller source</param>
        /// <param name="expression">Resource expression, like "labels, UCenter" or "UCenter"</param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string Resource(this Controller controller, string expression, params object[] args)
        {
            string virtualPath = "~/";
            return GetResource(controller.HttpContext, expression, virtualPath, args);
        }

        /// <summary>
        /// The implement method to get resource string from global or local resource object
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="expression">Resource expression, like "labels, UCenter" or "UCenter"</param>
        /// <param name="virtualPath">The virtual path</param>
        /// <param name="args"></param>
        /// <returns></returns>
        private static string GetResource(HttpContextBase httpContext, string expression, string virtualPath, object[] args)
        {
            ExpressionBuilderContext context = new ExpressionBuilderContext(virtualPath);
            ResourceExpressionBuilder builder = new ResourceExpressionBuilder();
            ResourceExpressionFields fields = builder.ParseExpression(expression, typeof(string), context) as ResourceExpressionFields;

            //Get global resource string
            if (!string.IsNullOrEmpty(fields.ClassKey))
                return string.Format((string)httpContext.GetGlobalResourceObject(fields.ClassKey,
                    fields.ResourceKey, CultureInfo.CurrentCulture), args);

            //Get local rescource string
            return string.Format((string)httpContext.GetLocalResourceObject(virtualPath, fields.ResourceKey,
                CultureInfo.CurrentCulture), args);
        }

        /// <summary>
        /// Organize the resource expression
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        private static string GetExpression(string expression)
        {
            if (expression.Contains(','))
                return expression;
            else
            {
                return "labels, " + expression;
            }
        }
    }
}